/*Copyright 2009-2010 by Ward, David J ( pymethius@ominian.net )
Licensed work, all rights reserved.*/
Ext.namespace("wm");
Ext.namespace("wmlib");

// test = new Ext.Panel({ layout: "anchor", title: "TestLayout", items: [{ title: "panel1", anchor: "100% 60%"}, {title:"panel2", anchor: "100% 40%"}]});
wmlib.konsole = function(){
    var self = this;
    
    var myRecord = Ext.data.Record.create([
        {name: "eventTS"},
        {name:"eventResponse"}
    ])
    
    var myStore = new Ext.data.Store({})

    //TODO - There is a much better way to do this with ExtJS but this works for now
    jQuery("head").append("<style> #konsolePanel dt { overflow: visible; white-space: normal }</style>");
    
    var responseList = {
        id: "konsoleResponse",
        xtype: "listview",
        multiSelect: false,
        emptyTest: "No events",
        reserveScrollOffset: true,
        autoScroll: true, //Apparently this does nothing here
        store: myStore,
        columns: [
                {
                    header: "TS",
                    width: "30%",
                    dataIndex: "eventTS"
                },
                {
                    header: "response",
                    width: "70%",
                    dataIndex: "eventResponse"
                }
            ]
    };
    
    var responsePanel = {
        xtype: "panel",
        anchor: "100% 70%",
        autoScroll: true,
        //setAutoScroll: true, //Keep our current line in view
        items: [responseList]
    }
    
    var handleResponse = function(data){
        wmlog.debug("Konsole - Response was " , data)
        
        toDisplay = data.result.escapeHTML().sub("\n","<br>\n",100)
        myStore.add( new myRecord({eventTS:"Now", eventResponse: toDisplay }))
        
    }
    
    var requestHandler = function(msg){
        //This should be an incoming request
        wmlog.info("Konsole - Request " , msg);
        toDisplay = String.escapeHTML(msg.params);
        myStore.add( new myRecord({eventTS: "Now", eventResponse: toDisplay}));
        
    }
    
    ctrlOn = false;
    var input = {
            id: "konsole-input",
            xtype: "textarea",
            anchor: "100% 30%",
            listeners: {                
                specialkey : function(field, e){                    
                    //wmlog.debug(e); //TODO - definitely want to kill this ASAP - I don` killed it
                    if (e.getKey() == e.CTRL){
                        ctrlOn = true;
                    }else{
                        if (e.getKey() == e.ENTER){                        
                        wm.router.makeRequest("konsole.run", field.getRawValue(), handleResponse);
                        }else{
                            ctrlOn = false;
                        }
                    }
                    if (e.getKey() == e.UP){
                        
                    }
                },
                rowdblclick : function(dblclk, field, e){
                    wmlog.log("I was double clicked!")
                }
            }
        };
      
    
    var main = new Ext.Window({
        id : "konsolePanel",
        title : "Konsole",
        layout: "anchor",
        minWidth: 450,
        width: 450,
        minHeight: 225,
        height: 225,
        items: [responsePanel, input]        
    })
    
    

    
    
    self.run = function(manager){
        wm.router.modules['konsole'] = requestHandler;
        main.show()
    }
    
    self.stop = function(){
        delete wm.router.modules['konsole'];
        main.hide()
    }
    
    wm.sys.register("Console", self);
}

Ext.onReady(function(){
    wm.loader.notify("konsole");
    new wmlib.konsole();
})